package com.clouddms.repository;

import com.clouddms.entity.UserOperationLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface UserOperationLogRepository extends JpaRepository<UserOperationLog, Long> {

    /**
     * 根据用户ID查找操作日志
     * @param userId 用户ID
     * @return 操作日志列表
     */
    @Query("SELECT log FROM UserOperationLog log JOIN FETCH log.user WHERE log.userId = :userId ORDER BY log.operationTime DESC")
    List<UserOperationLog> findByUserIdOrderByOperationTimeDesc(@Param("userId") Long userId);

    /**
     * 根据操作类型查找日志
     * @param operationType 操作类型
     * @return 操作日志列表
     */
    @Query("SELECT log FROM UserOperationLog log JOIN FETCH log.user WHERE log.operationType = :operationType ORDER BY log.operationTime DESC")
    List<UserOperationLog> findByOperationTypeOrderByOperationTimeDesc(@Param("operationType") String operationType);

    /**
     * 根据时间范围查找日志
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 操作日志列表
     */
    @Query("SELECT log FROM UserOperationLog log JOIN FETCH log.user WHERE log.operationTime BETWEEN :startTime AND :endTime ORDER BY log.operationTime DESC")
    List<UserOperationLog> findByOperationTimeBetweenOrderByOperationTimeDesc(
            @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 获取最近的操作日志
     * @return 最近的操作日志
     */
    @Query("SELECT log FROM UserOperationLog log JOIN FETCH log.user ORDER BY log.operationTime DESC")
    List<UserOperationLog> findRecentLogs();

    /**
     * 根据用户ID获取最近的操作日志
     * @param userId 用户ID
     * @return 最近的操作日志
     */
    @Query(value = "SELECT log FROM UserOperationLog log JOIN FETCH log.user WHERE log.userId = :userId ORDER BY log.operationTime DESC")
    List<UserOperationLog> findRecentLogsByUserId(@Param("userId") Long userId);

    /**
     * 统计用户操作次数
     * @param userId 用户ID
     * @param operationType 操作类型
     * @return 操作次数
     */
    @Query("SELECT COUNT(log) FROM UserOperationLog log WHERE log.userId = :userId AND log.operationType = :operationType")
    long countByUserIdAndOperationType(@Param("userId") Long userId, @Param("operationType") String operationType);

    /**
     * 统计时间范围内的操作次数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 操作次数
     */
    @Query("SELECT COUNT(log) FROM UserOperationLog log WHERE log.operationTime BETWEEN :startTime AND :endTime")
    long countByOperationTimeBetween(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 删除指定时间之前的日志
     * @param beforeTime 时间阈值
     * @return 删除的记录数
     */
    @Query("DELETE FROM UserOperationLog log WHERE log.operationTime < :beforeTime")
    int deleteByOperationTimeBefore(@Param("beforeTime") LocalDateTime beforeTime);
}